package com.lysh.aliHttp.model;

import com.lysh.aliHttp.common.enums.CannedAccessControlList;
import com.lysh.aliHttp.common.enums.StorageClass;
import com.lysh.aliHttp.utils.DateUtil;

import java.text.ParseException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liyishan
 * @date 2023/5/5 09:48
 * @apiNote
 */

public class ObjectMetadata {
    private Map<String, String> userMetadata = new HashMap();
    private Map<String, Object> metadata = new HashMap();
    public static final String AES_256_SERVER_SIDE_ENCRYPTION = "AES256";

    public ObjectMetadata() {
    }

    public Map<String, String> getUserMetadata() {
        return this.userMetadata;
    }

    public void setUserMetadata(Map<String, String> userMetadata) {
        this.userMetadata.clear();
        if (userMetadata != null && !userMetadata.isEmpty()) {
            this.userMetadata.putAll(userMetadata);
        }

    }

    public void setHeader(String key, Object value) {
        this.metadata.put(key, value);
    }

    public void addUserMetadata(String key, String value) {
        this.userMetadata.put(key, value);
    }

    public Date getLastModified() {
        return (Date)this.metadata.get("Last-Modified");
    }

    public void setLastModified(Date lastModified) {
        this.metadata.put("Last-Modified", lastModified);
    }

    public Date getExpirationTime() throws ParseException {
        return DateUtil.parseRfc822Date((String)this.metadata.get("Expires"));
    }

    public String getRawExpiresValue() {
        return (String)this.metadata.get("Expires");
    }

    public void setExpirationTime(Date expirationTime) {
        this.metadata.put("Expires", DateUtil.formatRfc822Date(expirationTime));
    }

    public long getContentLength() {
        Long contentLength = (Long)this.metadata.get("Content-Length");
        return contentLength == null ? 0L : contentLength;
    }

    public void setContentLength(long contentLength) {
        this.metadata.put("Content-Length", contentLength);
    }

    public String getContentType() {
        return (String)this.metadata.get("Content-Type");
    }

    public void setContentType(String contentType) {
        this.metadata.put("Content-Type", contentType);
    }

    public String getContentMD5() {
        return (String)this.metadata.get("Content-MD5");
    }

    public void setContentMD5(String contentMD5) {
        this.metadata.put("Content-MD5", contentMD5);
    }

    public String getContentEncoding() {
        return (String)this.metadata.get("Content-Encoding");
    }

    public void setContentEncoding(String encoding) {
        this.metadata.put("Content-Encoding", encoding);
    }

    public String getCacheControl() {
        return (String)this.metadata.get("Cache-Control");
    }

    public void setCacheControl(String cacheControl) {
        this.metadata.put("Cache-Control", cacheControl);
    }

    public String getContentDisposition() {
        return (String)this.metadata.get("Content-Disposition");
    }

    public void setContentDisposition(String disposition) {
        this.metadata.put("Content-Disposition", disposition);
    }

    public String getETag() {
        return (String)this.metadata.get("ETag");
    }

    public String getServerSideEncryption() {
        return (String)this.metadata.get("x-oss-server-side-encryption");
    }

    public void setServerSideEncryption(String serverSideEncryption) {
        this.metadata.put("x-oss-server-side-encryption", serverSideEncryption);
    }

    public String getObjectType() {
        return (String)this.metadata.get("x-oss-object-type");
    }

    public void setObjectAcl(CannedAccessControlList cannedAcl) {
        this.metadata.put("x-oss-object-acl", cannedAcl != null ? cannedAcl.toString() : "");
    }

    public Map<String, Object> getRawMetadata() {
        return Collections.unmodifiableMap(this.metadata);
    }

    public String getRequestId() {
        return (String)this.metadata.get("x-oss-request-id");
    }

    public StorageClass getObjectStorageClass() {
        String storageClassString = (String)this.metadata.get("x-oss-storage-class");
        return storageClassString != null ? StorageClass.parse(storageClassString) : StorageClass.Standard;
    }

    public String getObjectRawRestore() {
        return (String)this.metadata.get("x-oss-restore");
    }

    public boolean isRestoreCompleted() {
        String restoreString = this.getObjectRawRestore();
        if (restoreString == null) {
            throw new NullPointerException();
        } else {
            return !restoreString.equals("ongoing-request=\"true\"");
        }
    }
}
